using Autofac;
using CoreWebSimple.JWT;
using DotNetExtensions.Core.Autofac;
using DotNetExtensions.Core.Quartz;
using DotNetExtensions.Core.WebApi.Filters;
using DotNetExtensions.Core.WebApi.Jwt;
using DotNetExtensions.Core.WebApi.Middlewares;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreWebSimple
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddControllers(option=>
            {
                option.Filters.Add<GlobalExceptionFilter>();
                option.Filters.Add<JWTAuthorizationActionFilter>();

            }).AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.Converters.Add(new IsoDateTimeConverter()
                {
                    DateTimeFormat = "yyyy/MM/dd HH:mm:ss"
                });
            });
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Core.Web.Simple", Version = "v1" });
            });
            services.AddJwt<UserModel>(JwtOption.Default);
            services.AddMemoryCache(option=>
            {

            });
        }
        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegistTypes(option =>
            {
                option.Pattern.Add("CoreWebSimple*.dll");
                option.Pattern.Add("CoreSimple*.dll");
                //option.Filters.Add(new DotNetExtensions.Core.IOC.InjectTypeFilter { Kind = DotNetExtensions.Core.IOC.InjectKind.Named, Type = typeof(Named) });
                option.Filters.Add(new DotNetExtensions.Core.IOC.InjectTypeFilter { Kind = DotNetExtensions.Core.IOC.InjectKind.Named, Type = typeof(IJob) });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Core.Web.Simple"));
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseMiddleware<GlobalExceptionHandler>();
            app.UseAutofac(RegistTypeResolver.Default);
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseAuthentication();
            app.UseRouting();            
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "api/{controller}/{action=Index}/{id?}");
            });

            app.UseQuartz<IJob>();
        }
    }
}
